package com.quectel.business.common.forum;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.core.module.forum.dto.ForumTopicBindingDto;
import com.quectel.core.module.forum.dto.ForumTopicDto;
import com.quectel.core.module.forum.service.ForumTopicBindingService;
import com.quectel.core.module.forum.service.ForumTopicService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/5/16 10:14
 * @description new java file header...
 */
@Component
public class ForumTopicBusiness {
    @DubboReference
    private VillageService villageService;
    @DubboReference
    private ForumTopicService forumTopicService;
    @DubboReference
    private ForumTopicBindingService forumTopicBindingService;

    /**
     * 根据租户查询话题列表
     *
     * @param tenantId
     * @return
     */
    public List<ForumTopicDto> getAll(Long tenantId, Map<String, Object> topicOptions) {
        topicOptions.put(LambdaUtil.getFieldName(ForumTopicDto::getTenantId), String.valueOf(tenantId));
        return forumTopicService.queryList(topicOptions);
    }

    /**
     * 根据租户和城市查询话题列表
     *
     * @param tenantId
     * @param cityCode
     * @return
     */
    public List<ForumTopicDto> getCityTopic(Long tenantId, String cityCode, Map<String, Object> topicOptions) {
        Map<String, Object> params = new HashMap<>(2);
        params.put(LambdaUtil.getFieldName(VillageDto::getTenantId), String.valueOf(tenantId));
        params.put(LambdaUtil.getFieldName(VillageDto::getCityCode), cityCode);
        int total = villageService.queryTotal(params);
        if (total == 0) {
            return Collections.emptyList();
        }
        List<VillageDto> villageDtos = villageService.queryList(params);
        List<Long> villageList = villageDtos.stream().map(VillageDto::getId).collect(Collectors.toList());
        return getVillageTopic(villageList, topicOptions);
    }

    /**
     * 根据小区Id列表查询话题列表
     *
     * @param villageIds
     * @return
     */
    public List<ForumTopicDto> getVillageTopic(List<Long> villageIds, Map<String, Object> topicOptions) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("villageIds", villageIds);
        List<ForumTopicBindingDto> forumTopicBindingDtos = forumTopicBindingService.queryList(params);

        if (CollectionUtils.isEmpty(forumTopicBindingDtos)) {
            return Collections.emptyList();
        }
        Set<Long> collect = forumTopicBindingDtos.stream().map(ForumTopicBindingDto::getForumTopicId).collect(Collectors.toSet());

        List<ForumTopicDto> forumTopicDtos = new ArrayList<>(collect.size());

        for (Long topicId : collect) {
            topicOptions.put(LambdaUtil.getFieldName(ForumTopicDto::getId), topicId);
            int total = forumTopicService.queryTotal(topicOptions);
            if(total>0){
                forumTopicDtos.add(forumTopicService.selectCacheById(topicId));
            }
        }
        return forumTopicDtos;
    }
}
